#include <iostream>
using namespace std;

template <typename T>
class Allocator
{
public:
    T *allocator(size_t size) // 负责内存开辟,size个数量
    {
        return (T *)malloc(sizeof(T) * size);
    }
    void deallocator(T *p) // 内存释放
    {
        free(p);
    }
    void construct(T *p, const T &val) // 对象创建
    {
        // 更多new初始化方法 https://blog.csdn.net/qq_45041871/article/details/132251733
        new (p) T(val); // 使用的是定位new，将val传到地址为p的空间去，p地址指向val
    }
    void destroy(T *p) // 对象析构
    {
        p->~T();
    }
};
template <typename T, typename Alloc = Allocator<T>>
class Vector
{
public:
    Vector(int size = 5)
    {
        // 需要将内存开辟和对象构造分开处理
        // _first = new T[size];
        _first = _allocator.allocator(size); // 只开辟空间
        _last = _first;
        _end = _first + size; // 这就相当于指针遍历移动
    }
    ~Vector()
    {
        // 析构容器中有效的元素，然后释放_first指针所指向的内存
        // delete[] _first;
        for (T *p = _first; p != _last; p++)
        {
            _allocator.destroy(p);
        }
        _allocator.deallocator(_first);
        _first = _last = _end = nullptr;
    }
    Vector(const Vector<T> &src)
    {
        int size = src._end - src._first;
        // _first = new T[size];
        _first = _allocator.allocator(size);
        for (int i = 0; i < src._last - src._first; i++)
        {
            // _first[i] = src._first[i];
            _allocator.construct(_first + i, src._first[i]);
        }
        _last = _first + src._last - src._first;
        _end = _first + size;
    }
    Vector<T> &operator=(const Vector<T> &src)
    {
        if (this == &src)
            return *this;
        // delete[] _first;
        for (T *p = _first; p != _last; p++)
        {
            _allocator.destroy(p);
        }
        _allocator.deallocator(_first);

        int size = src._end - src._first;
        _first = new T[size];
        for (int i = 0; i < src._last - src._first; i++)
        {
            // _first[i] = src._first[i];
            _allocator.construct(_first + i, src._first[i]);
        }
        _last = _first + src._last - src._first;
        _end = _first + size;
        return *this;
    }
    void push_back(T &val)
    {
        if (full())
            expand();
        _allocator.construct(_last, val);
        _last++;
    }
    void pop_back()
    {
        if (empty())
            return;
        _last--;
        _allocator.destroy(_last);
    }
    T back() const
    {
        return *(_last - 1);
    }
    bool full() const { return _last == _end; }
    bool empty() const { return _last == _first; }
    int size() { return _last - _first; }
    T& operator[](int index)
    {
        if(index<0||index>size())
            throw "OutOfRangeException";
        return _first[index];
    }

    class iterator
    {
    public:
        iterator(T *ptr=nullptr):_ptr(ptr){}
        bool operator!=(const iterator &it) const{ return it._ptr != _ptr;}
        void operator++() { ++_ptr; }
        T &operator*() { return *_ptr; }
        const T &operator*() const { return *_ptr; }

    private:
        T *_ptr;
    };

    iterator begin() { return iterator(_first); }
    iterator end() { return iterator(_last); }

private:
    T *_first; // 指向数组起始的位置
    T *_last;  // 指向数组中有效元素的后继位置
    T *_end;   // 指向数组空间的后继位置

    iterator _iterator;
    Alloc _allocator; // 定义容器的空间配置器
    void expand()
    {
        int size = _end - _first;
        // T *temp = new T[size * 2];
        T *temp = _allocator.allocator(size * 2);
        for (int i = 0; i < size; i++)
        {
            // temp[i] = _first[i];
            _allocator.construct(temp + i, _first[i]);
        }
        // delete[] _first;
        for (T *p = _first; p != _last; p++)
        {
            _allocator.destroy(p);
        }
        _allocator.deallocator(_first);

        _first = temp;
        _last = _first + size;
        _end = _last + size;
    }
};

class Test
{
public:
    Test() { cout << "Test()" << endl; }
    ~Test() { cout << "~Test()" << endl; }
    Test(const Test &val) { cout << "Test(const Test& val)" << endl; }
};

int main()
{
    Vector<int> v;
    for (int i = 0; i < 20;i++)
    {
        v.push_back(i);
    }
    int size = v.size();
    for (int i = 0; i < size;i++)
    {
        cout << v[i] << " ";
    }
    cout << endl;

    Vector<int>::iterator it = v.begin();
    for (; it != v.end();++it)
    {
        cout << *it << " ";
    }
    cout<<endl;

    return 0;
}